```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CompletableFuture - Java异步编程的瑞士军刀</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background-color: #f8fafc;
            color: #1e293b;
            line-height: 1.6;
        }
        .hero {
            background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
        }
        .code-block {
            background-color: #1e293b;
            border-radius: 0.5rem;
            position: relative;
        }
        .code-block pre {
            margin: 0;
            padding: 1.5rem;
            overflow-x: auto;
        }
        .code-block .copy-btn {
            position: absolute;
            right: 0.5rem;
            top: 0.5rem;
            background-color: rgba(255, 255, 255, 0.1);
            border-radius: 0.25rem;
            padding: 0.25rem 0.5rem;
            color: white;
            cursor: pointer;
            transition: all 0.2s;
        }
        .code-block .copy-btn:hover {
            background-color: rgba(255, 255, 255, 0.2);
        }
        .method-card {
            transition: all 0.3s ease;
            border-left: 4px solid #4f46e5;
        }
        .method-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
        }
        .highlight {
            background-color: rgba(79, 70, 229, 0.1);
            padding: 0.25rem 0.5rem;
            border-radius: 0.25rem;
            font-weight: 500;
        }
        .quote {
            border-left: 4px solid #4f46e5;
            background-color: #f8fafc;
            padding: 1rem 1.5rem;
            font-style: italic;
            color: #475569;
        }
        h1, h2, h3, h4 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 700;
            color: #1e293b;
        }
        h1 {
            letter-spacing: -0.025em;
        }
        .footer {
            background-color: #1e293b;
        }
        .footer a:hover {
            color: #818cf8;
        }
        .mermaid {
            background-color: white;
            padding: 1.5rem;
            border-radius: 0.5rem;
            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
        }
    </style>
</head>
<body class="antialiased">
    <!-- Hero Section -->
    <section class="hero text-white py-20 px-4">
        <div class="container mx-auto max-w-4xl">
            <div class="flex items-center">
                <div class="w-full md:w-2/3">
                    <div class="flex items-center mb-4">
                        <span class="bg-white text-indigo-600 px-3 py-1 rounded-full text-sm font-semibold mr-2">Java 8</span>
                        <span class="text-indigo-200">异步编程</span>
                    </div>
                    <h1 class="text-4xl md:text-5xl font-bold mb-6 leading-tight">CompletableFuture - Java异步编程的瑞士军刀</h1>
                    <p class="text-xl text-indigo-100 mb-8">它不仅是一个工具，更是一种思维方式的转变。Java 8引入它，简直是给异步编程注入了一剂强心针。</p>
                    <div class="flex items-center space-x-4">
                        <div class="flex items-center">
                            <i class="fas fa-code-branch mr-2"></i>
                            <span>异步任务组合</span>
                        </div>
                        <div class="flex items-center">
                            <i class="fas fa-bolt mr-2"></i>
                            <span>非阻塞操作</span>
                        </div>
                    </div>
                </div>
                <div class="hidden md:block md:w-1/3">
                    <div class="mermaid">
                        graph LR
                            A[Async Task] --> B[thenApply]
                            B --> C[thenCombine]
                            C --> D[thenAccept]
                            D --> E[Result]
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto max-w-4xl px-4 py-12">
        <!-- Introduction -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <div class="h-1 bg-gradient-to-r from-indigo-500 to-purple-600 w-12 rounded-full mr-4"></div>
                <h2 class="text-3xl font-bold">CompletableFuture的魔力</h2>
            </div>
            <p class="text-lg text-slate-700 mb-6">CompletableFuture是Java 8中<code class="highlight">java.util.concurrent</code>包的一部分，它不仅实现了Future接口，还大大增强了异步编程的能力。想象一下，你可以在不阻塞主线程的情况下，轻松地处理异步任务，甚至可以像搭积木一样组合多个任务。是不是很酷？</p>
            
            <div class="quote mb-8">
                <p>"CompletableFuture一直是我向学生和同事们推荐的'瑞士军刀'。"</p>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8 mb-8">
                <div>
                    <h3 class="text-xl font-semibold mb-4 flex items-center">
                        <i class="fas fa-bolt text-indigo-500 mr-2"></i> 异步任务
                    </h3>
                    <p>通过<code class="highlight">supplyAsync()</code>或<code class="highlight">runAsync()</code>，你可以启动一个异步任务，让它在后台线程中执行，避免阻塞当前线程。</p>
                </div>
                <div>
                    <h3 class="text-xl font-semibold mb-4 flex items-center">
                        <i class="fas fa-puzzle-piece text-indigo-500 mr-2"></i> 组合任务
                    </h3>
                    <p><code class="highlight">thenApply()</code>、<code class="highlight">thenCombine()</code>等方法让你可以组合多个任务，形成一个流畅的链式操作。</p>
                </div>
            </div>
            
            <div class="mermaid my-8">
                flowchart TD
                    A[Start] --> B(supplyAsync)
                    B --> C{Success?}
                    C -->|Yes| D[thenApply]
                    C -->|No| E[completeExceptionally]
                    D --> F[thenCombine]
                    F --> G[Complete]
            </div>
        </section>
        
        <!-- Constructors -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <div class="h-1 bg-gradient-to-r from-indigo-500 to-purple-600 w-12 rounded-full mr-4"></div>
                <h2 class="text-3xl font-bold">构造函数</h2>
            </div>
            <p class="text-lg text-slate-700 mb-6">CompletableFuture有多个构造函数，但最常用的还是无参数构造和带初始值的构造。无参数构造创建一个空的CompletableFuture，而带初始值的构造则直接存储传入的结果。</p>
            
            <div class="code-block mb-6">
                <button class="copy-btn" onclick="copyCode(this)"><i class="fas fa-copy mr-1"></i>复制</button>
                <pre class="text-gray-300"><code>public class CompletableFuture&lt;T&gt; implements Future&lt;T&gt; {
    private Object result;
    private final Object lock = new Object();

    public CompletableFuture() {
        this.result = null;
    }

    public CompletableFuture(T value) {
        this.result = value;
    }
}</code></pre>
            </div>
            
            <p class="text-slate-700">这种设计非常灵活。你可以选择在任务完成后再设置结果，也可以在创建时就提供一个初始值，简直是为各种场景量身定制。</p>
        </section>
        
        <!-- Methods Section -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <div class="h-1 bg-gradient-to-r from-indigo-500 to-purple-600 w-12 rounded-full mr-4"></div>
                <h2 class="text-3xl font-bold">核心方法详解</h2>
            </div>
            
            <!-- complete() -->
            <div class="method-card bg-white rounded-lg shadow-md p-6 mb-8">
                <div class="flex items-center mb-4">
                    <div class="bg-indigo-100 text-indigo-800 p-2 rounded-lg mr-4">
                        <i class="fas fa-check-circle text-xl"></i>
                    </div>
                    <h3 class="text-2xl font-semibold">complete()</h3>
                </div>
                <p class="text-slate-700 mb-4"><code class="highlight">complete()</code>方法是CompletableFuture的核心之一。它用于将任务标记为完成，并传入一个计算结果。完成后，所有等待该任务的线程将会被唤醒。</p>
                
                <div class="code-block mb-4">
                    <button class="copy-btn" onclick="copyCode(this)"><i class="fas fa-copy mr-1"></i>复制</button>
                    <pre class="text-gray-300"><code>public boolean complete(T value) {
    synchronized (lock) {
        if (result != null) {
            return false;
        }
        result = value;
        notifyAll();
        return true;
    }
}</code></pre>
                </div>
                
                <p class="text-slate-700">在我参与的一个项目中，我们使用<code class="highlight">complete()</code>方法来处理异步任务的结果。这个方法就像是任务的终点，所有的努力都在这里汇聚。</p>
            </div>
            
            <!-- supplyAsync() -->
            <div class="method-card bg-white rounded-lg shadow-md p-6 mb-8">
                <div class="flex items-center mb-4">
                    <div class="bg-indigo-100 text-indigo-800 p-2 rounded-lg mr-4">
                        <i class="fas fa-bolt text-xl"></i>
                    </div>
                    <h3 class="text-2xl font-semibold">supplyAsync()</h3>
                </div>
                <p class="text-slate-700 mb-4"><code class="highlight">supplyAsync()</code>方法用于启动异步任务。它会在后台线程中执行任务，避免阻塞当前线程。</p>
                
                <div class="code-block mb-4">
                    <button class="copy-btn" onclick="copyCode(this)"><i class="fas fa-copy mr-1"></i>复制</button>
                    <pre class="text-gray-300"><code>public static &lt;U&gt; CompletableFuture&lt;U&gt; supplyAsync(Supplier&lt;U&gt; supplier) {
    CompletableFuture&lt;U&gt; future = new CompletableFuture&lt;&gt;();
    ForkJoinPool.commonPool().submit(() -> {
        try {
            U result = supplier.get();
            future.complete(result);
        } catch (Throwable ex) {
            future.completeExceptionally(ex);
        }
    });
    return future;
}</code></pre>
                </div>
                
                <p class="text-slate-700"><code class="highlight">supplyAsync()</code>是异步任务的起点。通过它，你可以轻松地启动一个异步任务，而不必担心线程管理的问题。</p>
            </div>
            
            <!-- thenApply() -->
            <div class="method-card bg-white rounded-lg shadow-md p-6 mb-8">
                <div class="flex items-center mb-4">
                    <div class="bg-indigo-100 text-indigo-800 p-2 rounded-lg mr-4">
                        <i class="fas fa-arrow-right text-xl"></i>
                    </div>
                    <h3 class="text-2xl font-semibold">thenApply()</h3>
                </div>
                <p class="text-slate-700 mb-4"><code class="highlight">thenApply()</code>方法用于组合任务。它会在当前任务完成后执行提供的函数，并将结果传递给下一个CompletableFuture。</p>
                
                <div class="code-block mb-4">
                    <button class="copy-btn" onclick="copyCode(this)"><i class="fas fa-copy mr-1"></i>复制</button>
                    <pre class="text-gray-300"><code>public &lt;U&gt; CompletableFuture&lt;U&gt; thenApply(Function&lt;? super T, ? extends U&gt; fn) {
    CompletableFuture&lt;U&gt; d = new CompletableFuture&lt;&gt;();
    whenComplete((result, ex) -> {
        if (ex == null) {
            try {
                U newResult = fn.apply(result);
                d.complete(newResult);
            } catch (Throwable e) {
                d.completeExceptionally(e);
            }
        } else {
            d.completeExceptionally(ex);
        }
    });
    return d;
}</code></pre>
                </div>
                
                <p class="text-slate-700"><code class="highlight">thenApply()</code>就像是任务的"接力棒"。通过它，你可以将多个任务串联起来，形成一个流畅的任务链。</p>
            </div>
            
            <!-- join() and get() -->
            <div class="method-card bg-white rounded-lg shadow-md p-6 mb-8">
                <div class="flex items-center mb-4">
                    <div class="bg-indigo-100 text-indigo-800 p-2 rounded-lg mr-4">
                        <i class="fas fa-download text-xl"></i>
                    </div>
                    <h3 class="text-2xl font-semibold">join()和get()</h3>
                </div>
                <p class="text-slate-700 mb-4"><code class="highlight">join()</code>和<code class="highlight">get()</code>都用于获取CompletableFuture计算的结果，但它们之间有一些区别。<code class="highlight">join()</code>会抛出CompletionException，而<code class="highlight">get()</code>会抛出ExecutionException。</p>
                
                <div class="code-block mb-4">
                    <button class="copy-btn" onclick="copyCode(this)"><i class="fas fa-copy mr-1"></i>复制</button>
                    <pre class="text-gray-300"><code>public T join() {
    try {
        return get();
    } catch (ExecutionException ex) {
        throw new CompletionException(ex.getCause());
    }
}

public T get() throws InterruptedException, ExecutionException {
    if (result == null) {
        synchronized (lock) {
            while (result == null) {
                wait();
            }
        }
    }
    return (T) result;
}</code></pre>
                </div>
                
                <p class="text-slate-700"><code class="highlight">join()</code>和<code class="highlight">get()</code>就像是获取结果的两种不同方式。选择哪种方式，取决于你对异常处理的偏好。</p>
            </div>
            
            <!-- completeExceptionally() -->
            <div class="method-card bg-white rounded-lg shadow-md p-6">
                <div class="flex items-center mb-4">
                    <div class="bg-indigo-100 text-indigo-800 p-2 rounded-lg mr-4">
                        <i class="fas fa-exclamation-triangle text-xl"></i>
                    </div>
                    <h3 class="text-2xl font-semibold">completeExceptionally()</h3>
                </div>
                <p class="text-slate-700 mb-4">当异步任务发生异常时，可以通过<code class="highlight">completeExceptionally()</code>方法将异常传递给CompletableFuture的消费者，通知其计算失败。</p>
                
                <div class="code-block mb-4">
                    <button class="copy-btn" onclick="copyCode(this)"><i class="fas fa-copy mr-1"></i>复制</button>
                    <pre class="text-gray-300"><code>public boolean completeExceptionally(Throwable ex) {
    synchronized (lock) {
        if (result != null) {
            return false;
        }
        result = new CompletionException(ex);
        notifyAll();
        return true;
    }
}</code></pre>
                </div>
                
                <p class="text-slate-700"><code class="highlight">completeExceptionally()</code>是异常处理的利器。通过它，你可以轻松地将异常传递给下游任务，确保异常不会被悄悄吞掉。</p>
            </div>
        </section>
        
        <!-- Best Practices -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <div class="h-1 bg-gradient-to-r from-indigo-500 to-purple-600 w-12 rounded-full mr-4"></div>
                <h2 class="text-3xl font-bold">最佳实践</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div class="bg-white p-6 rounded-lg shadow-md">
                    <h3 class="text-xl font-semibold mb-4 flex items-center">
                        <i class="fas fa-lightbulb text-indigo-500 mr-2"></i> 链式调用
                    </h3>
                    <p class="text-slate-700 mb-4">利用CompletableFuture丰富的组合方法，将多个异步操作优雅地串联起来，形成清晰的业务逻辑流程。</p>
                    <div class="mermaid">
                        graph LR
                            A[请求数据] --> B[处理数据]
                            B --> C[转换格式]
                            C --> D[保存结果]
                    </div>
                </div>
                
                <div class="bg-white p-6 rounded-lg shadow-md">
                    <h3 class="text-xl font-semibold mb-4 flex items-center">
                        <i class="fas fa-shield-alt text-indigo-500 mr-2"></i> 异常处理
                    </h3>
                    <p class="text-slate-700 mb-4">合理使用<code class="highlight">completeExceptionally()</code>、<code class="highlight">exceptionally()</code>等方法，确保异步任务中的异常能够被正确捕获和处理。</p>
                    <div class="mermaid">
                        graph TD
                            A[开始] --> B[执行任务]
                            B --> C{成功?}
                            C -->|是| D[处理结果]
                            C -->|否| E[处理异常]
                    </div>
                </div>
            </div>
        </section>
    </main>
    
    <!-- Footer -->
    <footer class="footer text-white py-8 px-4">
        <div class="container mx-auto max-w-4xl">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <h3 class="text-xl font-semibold">技术小馆</h3>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-gray-300 hover:text-white transition-colors duration-200">
                        <i class="fas fa-globe mr-2"></i> http://www.yuque.com/jtostring
                    </a>
                </div>
            </div>
        </div>
    </footer>

    <script>
        // Initialize Mermaid
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
        
        // Copy code function
        function copyCode(button) {
            const codeBlock = button.parentElement;
            const code = codeBlock.querySelector('code').textContent;
            navigator.clipboard.writeText(code).then(() => {
                const originalText = button.innerHTML;
                button.innerHTML = '<i class="fas fa-check mr-1"></i>已复制';
                setTimeout(() => {
                    button.innerHTML = originalText;
                }, 2000);
            });
        }
        
        // Add smooth scrolling to all links
        document.querySelectorAll('a[href^="#"]').forEach(anchor => {
            anchor.addEventListener('click', function (e) {
                e.preventDefault();
                document.querySelector(this.getAttribute('href')).scrollIntoView({
                    behavior: 'smooth'
                });
            });
        });
    </script>
</body>
</html>
```